package com.baijiayun.live.ui.speakpanel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.baijiayun.live.ui.base.BaseViewModel
import com.baijiayun.live.ui.base.RouterViewModel
import com.baijiayun.livecore.context.LPConstants
import com.baijiayun.livecore.models.LPInteractionAwardModel
import com.baijiayun.livecore.models.LPMediaModel
import com.baijiayun.livecore.models.LPUserModel
import com.baijiayun.livecore.models.imodels.IMediaModel
import com.baijiayun.livecore.models.imodels.IUserInModel
import com.baijiayun.livecore.models.imodels.IUserModel
import com.baijiayun.livecore.viewmodels.impl.LPSpeakQueueViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit

/**
 * Created by yongjiaming on 2019-10-17
 * Describe:
 */
class SpeakViewModel(val routerViewModel: RouterViewModel) : BaseViewModel() {

    val singleSpeakerChange = MutableLiveData<Pair<Boolean, IUserModel>>()
    override fun subscribe() {
        with(routerViewModel) {
            liveRoom.speakQueueVM.observableOfActiveUsers
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : DisposingObserver<List<IMediaModel>>() {
                        override fun onNext(list: List<IMediaModel>) {
                            for (mediaModel in list) {
                                notifyRemotePlayableChanged.value = mediaModel
                                if (!mediaModel.hasExtraStreams()) {
                                    continue
                                }
                                for (extMediaModel in mediaModel.extraStreams) {
                                    if (extMediaModel.mediaSourceType == LPConstants.MediaSourceType.ExtCamera || extMediaModel.mediaSourceType == LPConstants.MediaSourceType.ExtScreenShare) {
                                        notifyRemotePlayableChanged.value = extMediaModel
                                    }
                                }
                            }
                        }
                    })

            liveRoom.speakQueueVM.observableOfMediaPublish
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : DisposingObserver<IMediaModel>() {
                        override fun onNext(t: IMediaModel) {
                            notifyRemotePlayableChanged.value = t
                        }
                    })

            liveRoom.speakQueueVM.observableOfPresenterChange.toObservable()
                    .throttleLast(1, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : DisposingObserver<String>() {
                        override fun onNext(s: String) {
                            if (TextUtils.isEmpty(s)) {
                                notifyPresenterChange.value = s to LPMediaModel()
                                return
                            }
                            var defaultMediaModel: IMediaModel? = null
                            var extMediaModel: IMediaModel? = null
                            for (mediaModel in liveRoom.speakQueueVM.speakQueueList) {
                                if (mediaModel.user.userId == s) {
                                    if (mediaModel.mediaSourceType == LPConstants.MediaSourceType.MainCamera) {
                                        defaultMediaModel = mediaModel
                                        break
                                    } else {
                                        extMediaModel = mediaModel
                                    }
                                }
                            }

                            if (defaultMediaModel == null) {
                                //兼容只有辅助摄像头没有老师的情况
                                if (extMediaModel == null) {
                                    defaultMediaModel = LPMediaModel()
                                } else {
                                    val mediaModel = LPMediaModel()
                                    mediaModel.user = extMediaModel.user as LPUserModel?
                                    mediaModel.userId = extMediaModel.user.userId
                                    mediaModel.keepAlive = extMediaModel.isKeepAlive
                                    mediaModel.audioOn = false
                                    mediaModel.videoOn = false
                                    mediaModel.mediaId = ""
                                    defaultMediaModel = mediaModel
                                }
                            }
                            if (defaultMediaModel.user == null) {
                                var userModel: IUserModel? = liveRoom.onlineUserVM.getUserById(s)
                                if (userModel == null && s == LPSpeakQueueViewModel.FAKE_MIX_STREAM_USER_ID) {
                                    userModel = liveRoom.speakQueueVM.mixedStreamingModel.user
                                    (defaultMediaModel as LPMediaModel).videoOn = liveRoom.speakQueueVM.mixedStreamingModel.isVideoOn
                                    defaultMediaModel.audioOn = liveRoom.speakQueueVM.mixedStreamingModel.isAudioOn
                                }
                                (defaultMediaModel as LPMediaModel).user = userModel as LPUserModel?
                            }
                            if (defaultMediaModel.user == null) {
                                notifyPresenterChange.value = "" to defaultMediaModel
                            } else {
                                notifyPresenterChange.value = s to defaultMediaModel
                            }
                        }
                    })
            liveRoom.observableOfAward.observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : DisposingObserver<LPInteractionAwardModel>() {
                        override fun onNext(awardModel: LPInteractionAwardModel) {
                            awardRecord.clear()
                            awardRecord.putAll(awardModel.value.recordAward)
                            notifyAward.value = awardModel
                        }
                    })
            liveRoom.observableOfUserIn
                    .filter { it.user.type == LPConstants.LPUserType.Student }
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : DisposingObserver<IUserInModel>() {
                        override fun onNext(iUserInModel: IUserInModel) {
                            singleSpeakerChange.value = true to iUserInModel.user
                        }
                    })

            liveRoom.onlineUserVM.observableOfUserOut
                    .filter { it.type == LPConstants.LPUserType.Student || it.type == LPConstants.LPUserType.Visitor }
                    .subscribe(object : DisposingObserver<IUserModel>() {
                        override fun onNext(iUserModel: IUserModel) {
                            singleSpeakerChange.value = false to iUserModel
                        }
                    })
            liveRoom.speakQueueVM.observableOfPresenterIn
                    .subscribe(object : DisposingObserver<String>() {
                        override fun onNext(t: String) {
                            actionPresenterIn.value = t
                        }
                    })
            actionRequestActiveUsers.value = Unit
            if (liveRoom.awardValue != null) {
                awardRecord.putAll(liveRoom.awardValue.value.recordAward)
            }
        }
    }
}